package com.mobimagic.lockscreen.sdk;

import java.io.FileInputStream;

import com.mobimagic.lockscreen.MobileChargingHelper;
import com.mobimagic.lockscreen.view.MobileChargeColorHelper;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class LockScreenSDK {
    private static final String TAG = "LockScreenSDK";
    public static LockScreenSDK instance;
    private Context mContext;
    private String pkgName;
    private String processName;

    private ILockScreenHelper mLockScreenHelper;
    private ILockScreenAd mLockScreenAd;
    private ILockScreenBooster mLockScreenBooster;
    private ILockScreenCommon mLockScreenCommon;

    private LockScreenSDK() {

    }

    public static LockScreenSDK getInstance() {
        if (instance == null) {
            instance = new LockScreenSDK();
        }
        return instance;
    }

    public void init(Context context, ILockScreenHelper iLockScreenAdapter, ILockScreenAd lockScreenAd,
            ILockScreenCommon iLockScreenCommon, ILockScreenBooster iLockScreenBooster) {
        this.mContext = context;
        pkgName = context.getPackageName();
        this.mLockScreenHelper = iLockScreenAdapter;
        this.mLockScreenAd = lockScreenAd;
        this.mLockScreenCommon = iLockScreenCommon;
        this.mLockScreenBooster = iLockScreenBooster;
        processName = getCurrentProcessName();
    }

    public String getPkgName() {
        return pkgName;
    }

    public Context getContext() {
        return mContext;
    }

    public String getString(int res) {
        return mContext.getResources().getString(res);
    }

    public String[] getStringArray(int week) {
        return mContext.getResources().getStringArray(week);
    }

    public ILockScreenHelper getLockScreenHelper() {
        return mLockScreenHelper;
    }

    public ILockScreenAd getLockScreenAd() {
        return mLockScreenAd;
    }

    public ILockScreenCommon getLockScreenCommon() {
        return mLockScreenCommon;
    }

    public ILockScreenBooster getLockScreenBooster() {
        return mLockScreenBooster;
    }

    public String getProcessName() {
        return processName;
    }

    /** 返回当前的进程名 */
    private static String getCurrentProcessName() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("/proc/self/cmdline");

            byte[] buffer = new byte[32];

            int len = 0;
            int b;
            while ((b = fis.read()) > 0 && len < buffer.length) {
                buffer[len++] = (byte) b;
            }
            if (len > 0) {
                String s = new String(buffer, 0, len, "UTF-8");
                return s;
            }
        } catch (Exception e) {
            Log.e(TAG, "", e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                }
            }
        }

        return null;
    }

    public void onScreenOn() {
        MobileChargingHelper.getInstance().onScreenOn(false);
    }

    public void onScreenOff() {
        MobileChargingHelper.getInstance().onScreenOff(false);
    }

    public void onBatteryStatusChange(Intent intent) {
        MobileChargingHelper.getInstance().onBatteryStatusChange(intent);

    }
}
